package mysql

import (
	"gorm.io/gorm"
	"short-video-dev/videorpc/videorpc"
)

type VideoInfo struct {
	gorm.Model
	UserId        int    `gorm:"type:int(11);not null;comment:'上传用户ID'"`
	VideoTitle    string `gorm:"type:varchar(50);not null;comment:'视频标题'"`
	VideoCategory string `gorm:"type:varchar(50);not null;comment:'视频分类'"`
	VideoLink     string `gorm:"type:varchar(255);not null;comment:'视频URL'"`
	CoverLink     string `gorm:"type:varchar(255);not null;comment:'视频封面URL'"`
	VideoIntro    string `gorm:"type:varchar(500);not null;comment:'视频简介'"`
	PlayNum       int    `gorm:"type:int(11);not null;default:0;comment:'播放量'"`
	LikeNum       int    `gorm:"type:int(11);not null;default:0;comment:'点赞数'"`
	CollectNum    int    `gorm:"type:int(11);not null;default:0;comment:'收藏数'"`
	CommentNum    int    `gorm:"type:int(11);not null;default:0;comment:'评论数'"`
	VideoStatus   int    `gorm:"type:int(11);not null;default:1;comment:'视频状态'"`
	TopicId       int    `gorm:"type:int(11);not null;comment:'关联话题ID'"`
}

func (i *VideoInfo) VideoAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *VideoInfo) VideoFindById(db *gorm.DB, vid int64, userId int64) error {
	return db.Debug().Where("id=? and user_id=?", vid, userId).First(i).Error
}

func (i *VideoInfo) VideoStatusUp(db *gorm.DB) error {
	return db.Debug().Updates(i).Error
}

func (i *VideoInfo) VideoDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(i).Error
}

func (i *VideoInfo) VideoFindByOneId(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(i).Error
}
func VideoList(db *gorm.DB, in *videorpc.VideoListRequest) []*videorpc.VideoList {
	var list []*videorpc.VideoList
	tx := db.Debug().Model(&VideoInfo{}).Select("video_infos.video_title", "video_infos.cover_link",
		"video_infos.play_num", "user_infos.nike_name", "user_infos.avatar_link as head_image").
		Joins("left join user_infos on user_infos.id = video_infos.user_id")
	if in.SortType == 0 {
		tx = tx.Order("video_infos.play_num desc")
	}
	if in.SortType == 1 {
		tx = tx.Order("video_infos.created_at desc")
	}
	var count int64
	tx.Count(&count)
	tx.Scopes(Paginate(int(in.Page), int(in.Size))).Where("video_infos.video_status = 0").Find(&list)
	return list
}
func VideoDetail(db *gorm.DB, id int64) (*videorpc.VideoDetailResponse, error) {
	var list *videorpc.VideoDetailResponse
	err := db.Debug().Model(&VideoInfo{}).Select("video_infos.*", "user_infos.nike_name", "user_infos.avatar_link as head_image").
		Joins("left join user_infos on user_infos.id = video_infos.user_id").
		Where("video_infos.id = ?", id).First(&list).Error

	db.Model(&VideoInfo{}).Where("video_infos.id = ?", id).Update("video_infos.play_num",
		gorm.Expr("video_infos.play_num + ?", 1))

	return list, err
}
func VideoPush(db *gorm.DB) []*videorpc.VideoList {
	var list []*videorpc.VideoList
	db.Debug().Model(&VideoInfo{}).Select("video_infos.video_title", "video_infos.cover_link",
		"video_infos.play_num", "user_infos.nike_name", "user_infos.avatar_link as head_image").
		Joins("left join user_infos on user_infos.id = video_infos.user_id").
		Order("video_infos.play_num desc").Limit(3).
		Where("video_infos.video_status = 0").Find(&list)
	return list
}
